package com.etc.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.etc.entity.Article;
import com.etc.service.impl.ArticleServiceImpl;
import com.etc.util.PageData;
import com.google.gson.Gson;

/**
 * Servlet implementation class ArticleController
 */
@WebServlet("/ArticleController")
public class ArticleController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	//调用业务层
	ArticleServiceImpl service=new ArticleServiceImpl();  
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ArticleController() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置请求编码,位置需要放到获取值的前面
		/*request.setCharacterEncoding("utf-8");*/
		//获取参数 参数名op的值如果是等于query才做查询
		String op=request.getParameter("op");
		HttpSession session = request.getSession();
		Object object = session.getAttribute("users");
		if (object!=null) {//登录后的功能
			//1.管理员对文章的操作
			adminOp(op,request,response,session);

		}else if("frontQuery".equals(op)) {
			//2.普通用户对文章的查询操作
			frontQuery(op,request,response);

		}else if("frontQueryById".equals(op)) {
			//3.普通用户对文章的查询操作
			frontQueryById(op,request,response);
		}else if("keyWord".equals(op)) {
			//4.普通用户对文章的模糊查询操作
			frontQueryTopLike(op,request,response);
		}
		else {
			//访问的时候发现没有登录,跳转到登陆页面去
			response.sendRedirect("mvc/login.jsp");
		}

	}

	private void frontQueryTopLike(String op, HttpServletRequest request, HttpServletResponse response) throws IOException {
		//获取模糊查询的关键字
		String keyWord = request.getParameter("keyWord");
		//调用业务层的方法
		List<Article> list = service.getByTitle(keyWord);
		//把list通过ajax传到前台,通过gson把list转成json格式的字符串
		Gson gson = new Gson();
		String jsonString = gson.toJson(list);
		response.getWriter().print(jsonString);
		response.getWriter().close();
	}

	private void frontQueryById(String op, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//根据id调用业务层
		int articleId=Integer.parseInt(request.getParameter("articleId")) ;
		Article article = service.getArticleById(articleId);
		//将其存储到request作用域
		request.setAttribute("article", article);
		//转发到详情页
		request.getRequestDispatcher("front/detail.jsp").forward(request, response);
		
	}

	private void frontQuery(String op, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//查询前3条,调用业务层
		List<Article> list = service.getTopArticles();
		//将list对象存储到request作用域,在下一个页面中获取显示
		request.setAttribute("toplist", list);
		//跳转
		request.getRequestDispatcher("front/home.jsp").forward(request, response);

	}

	private void adminOp(String op, HttpServletRequest request, HttpServletResponse response, HttpSession session) throws ServletException, IOException {
		if("query".equals(op)) {
			//调用方法
			//创建service对象
			//调用查询方法
			List<Article> list = service.getArticles();
			//这里list对象应该渲染给jsp页面
			//1.传递数据给jsp页面,jsp页面做显示数据  传递数据:request.setAttribute("key", value);,到了下一个页面就可以通过对应的
			//request.getAttribute("key");
			request.setAttribute("list", list);//将list对象存放在request的作用域中,在下一个页面就可以获取list

			//2.进行页面跳转  forward转发
			request.getRequestDispatcher("mvc/showArticles.jsp").forward(request, response);

		}else if("add".equals(op)) {
			//文章标题
			String articleTitle=request.getParameter("articleTitle");//通过名字获取值，因为表单提交name=value
			//文章内容
			String articleContent=request.getParameter("articleContent");
			//文章作者
			String articleAuthor=request.getParameter("articleAuthor");

			Article article=new Article(articleTitle,articleContent,articleAuthor);
			//调用业务层
			//调用添加的方法
			boolean flag=service.addArticle(article);
			//转发到servlet而不是jsp  
			request.getRequestDispatcher("ArticleController?op=query").forward(request, response);

		}else if("del".equals(op)) {
			//获取用户传递过来的参数articleId
			String articleId = request.getParameter("articleId");
			//调用service
			boolean flag = service.delArticle(Integer.parseInt(articleId));
			//可以根据flag的值提示用户是否删除成功
			//页面跳转
			request.getRequestDispatcher("ArticleController?op=query").forward(request, response);
		}else if("update".equals(op)) {
			//获取模态框的所有数据
			int articleId = Integer.parseInt(request.getParameter("articleId"));
			//文章标题
			String articleTitle=request.getParameter("articleTitle");//通过名字获取值，因为表单提交name=value
			//文章内容
			String articleContent=request.getParameter("articleContent");
			//文章作者
			String articleAuthor=request.getParameter("articleAuthor");

			Article article = new Article(articleId, articleTitle, articleContent, articleAuthor);
			//调用业务层的修改方法
			service.updateArticle(article);
			//转发到servlet而不是jsp  
			request.getRequestDispatcher("ArticleController?op=query").forward(request, response);
		}else if("queryLike".equals(op)) {
			//1.获取模糊查询关键字
			String articleLike = request.getParameter("articleLike");
			//2.调用业务层的方法
			List<Article> list = service.getArticleByLike(articleLike);
			//3.将list传递给jsp页面，存储到request作用域
			request.setAttribute("list", list);
			//把模糊查询关键字存储到request作用域给jsp页面
			request.setAttribute("articleLike", articleLike);
			//4.进行页面跳转
			request.getRequestDispatcher("mvc/showArticles.jsp").forward(request, response);
		}else if("articleByPage_ajax".equals(op)) {
			//文章模糊查询的分页效果ajax
			articleByPage_ajax(request,response);
		}else if("update_ajax".equals(op)) {
			update_ajax(request,response);
		}else if("dele_ajax".equals(op)) {
			dele_ajax(request,response);
		}

	}

private void dele_ajax(HttpServletRequest request, HttpServletResponse response) throws IOException {
	//获取用户传递过来的参数articleId
	String articleId = request.getParameter("articleId");
	//调用service
	boolean flag = service.delArticle(Integer.parseInt(articleId));
	//通过ajx返回json数据
	Gson gson = new Gson();
    String jsonFlag = gson.toJson(flag);
    response.getWriter().print(jsonFlag);
    response.getWriter().close();	
	}

private void update_ajax(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 接收参数(此时ajax提交的参数是一个由js对象转换成的json格式字符串)
	    String jsonString = request.getParameter("article");
	    //通过gson的fromjson()转成java对象
	    Gson gson = new Gson();
	    Article article = gson.fromJson(jsonString, Article.class);
	    //调用业务层的修改方法
	    boolean flag = service.updateArticle(article);
	    //通过ajx返回json数据
	    String jsonFlag = gson.toJson(flag);
	    response.getWriter().print(jsonFlag);
	    response.getWriter().close();
		
	}

	private void articleByPage_ajax(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//1.默认页码和显示条数
		int pageNo=1;//默认值
		int pageSize=3;//默认值
		String titleKey="";//如果没有传递,默认查所有
		
		//2.如果前台有指定值就使用前台的数据,否则按默认显示
		if(request.getParameter("pageNo")!=null) {
			pageNo=Integer.parseInt(request.getParameter("pageNo"));
		}
		if(request.getParameter("pageSize")!=null) {
			pageSize=Integer.parseInt(request.getParameter("pageSize"));
		}
		if(request.getParameter("titleKey")!=null) {
			titleKey=request.getParameter("titleKey");
		}

		//3.调用业务层
		PageData pageData = service.getArticleByLikePage(pageNo, pageSize, titleKey);
		//4.通过ajax返回json数据
		Gson gson = new Gson();
		String json = gson.toJson(pageData);
		response.getWriter().print(json);
		response.getWriter().close();
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);//因为你不确定用户的入口
	}

}
